import 'package:efood_multivendor/controller/restaurant_controller.dart';
import 'package:efood_multivendor/data/api/api_checker.dart';
import 'package:efood_multivendor/data/api/api_client.dart';
import 'package:efood_multivendor/data/model/response/config_model.dart';
import 'package:efood_multivendor/data/model/response/order_review_model.dart';
import 'package:efood_multivendor/data/repository/splash_repo.dart';
import 'package:get/get.dart';

import 'auth_controller.dart';
import 'banner_controller.dart';
import 'campaign_controller.dart';
import 'category_controller.dart';
import 'filter_controller.dart';
import 'user_controller.dart';

class SplashController extends GetxController implements GetxService {
  final SplashRepo splashRepo;
  SplashController({required this.splashRepo});

  ConfigModel? _configModel;
  bool _firstTimeConnectionCheck = true;
  bool _hasConnection = true;
  String? _showType;
  String? _foodOrRestaurantId;
  String? _currentVersion;
  UnreviewedOrdersResponse? _unreviewedOrdersResponse;
  bool _isFirstOpen = true;
  //bool _isOnWalletScreen = false;

  ConfigModel? get configModel => _configModel;
  DateTime get currentTime => DateTime.now();
  bool get firstTimeConnectionCheck => _firstTimeConnectionCheck;
  bool get hasConnection => _hasConnection;
  String? get showType => _showType;
  String? get foodOrRestaurantId => _foodOrRestaurantId;
  String? get currentVersion => _currentVersion;
  UnreviewedOrdersResponse? get unreviewedOrdersResponse =>
      _unreviewedOrdersResponse;
  bool get isFirstOpen => _isFirstOpen;
  // bool get isOnWalletScreen => _isOnWalletScreen;

  Future<bool> getConfigData() async {
    _hasConnection = true;
    Response response = await splashRepo.getConfigData();
    bool _isSuccess = false;
    if (response.statusCode == 200) {
      _configModel = ConfigModel.fromJson(response.body);
      _isSuccess = true;
    } else {
      ApiChecker.checkApi(response);
      if (response.statusText == ApiClient.noInternetMessage) {
        _hasConnection = false;
      }
      _isSuccess = false;
    }
    update();
    return _isSuccess;
  }

  bool initSharedData() {
    return splashRepo.initSharedData();
  }

  bool showIntro() {
    return splashRepo.showIntro();
  }

  void disableIntro() {
    splashRepo.disableIntro();
  }

  void setFirstTimeConnectionCheck(bool isChecked) {
    _firstTimeConnectionCheck = isChecked;
  }

  void setNotificationPayload(String showType, String foodOrRestaurantId) {
    _showType = showType;
    _foodOrRestaurantId = foodOrRestaurantId;
    update();
  }

  Future<void> setVersion(String version) async {
    _currentVersion = version;
    update();
  }

  void setFirstOpen({bool firstOpen = false}) {
    _isFirstOpen = firstOpen;
  }

  Future<void> loadData(bool reload) async {
    List<Future> futures = [
      Get.find<BannerController>().getBannerList(reload),
      Get.find<CategoryController>().getCategoryList(reload),
      Get.find<CampaignController>().getItemCampaignList(reload,
          filter: Get.find<SplashController>()
              .configModel!
              .defaultCampaignFilter
              .toString()),
      Get.find<RestaurantController>().getRestaurantList('1', reload),
    ];

    if (Get.find<AuthController>().isLoggedIn()) {
      futures.add(Get.find<UserController>().getUserInfo());
    }

    await Future.wait(futures);

    Get.find<FilterController>().setPrice(
      Get.find<SplashController>().configModel?.minimumProductPrice?.toDouble(),
      Get.find<SplashController>().configModel?.maximumProductPrice?.toDouble(),
    );
  }

  // void setWalletScreen() {
  //   _isOnWalletScreen = !_isOnWalletScreen;
  //   update();
  // }
}
